/**
 * 中国9号健康产业有限公司
 * Copyright (C) JiuHaoHealth Corporation. All Rights Reserved
 */
package com.jiuhaohealth.kangxun360.account;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jiuhaohealth.common.page.PageModel;
import com.jiuhaohealth.common.util.JsonUtil;
import com.jiuhaohealth.common.util.Message;
import com.jiuhaohealth.kangxun360.main.po.Account;
/**
 * @author minghui(jackchenmh@gmail.com)
 * @version 2014-3-18 下午5:08:45
 */
@Controller
@RequestMapping("/")
public class AccountController {
	protected final static Logger logger = LoggerFactory.getLogger(AccountController.class);
	private final static Integer PAGE_SIZE = 10; 
	@Resource(name="accountService")
	private AccountService accountService;
	
	@RequestMapping(value = {"/account/list"}, method = RequestMethod.GET)
	public ModelAndView listAccount() {
		ModelAndView modelAndView = new ModelAndView("json");
		List<Account> accounts = accountService.queryAccounts("firstName1", "lastName");
		modelAndView.addObject("json", JsonUtil.listToJson(accounts));
		return modelAndView;
	}
	
	@RequestMapping(value = {"/account/listpage/{userId}/{pageNum}"}, method = RequestMethod.GET)
	public ModelAndView listPageAccount(
	       @PathVariable("userId")String userId , 
	       @PathVariable("pageNum")Integer pageNum ,
	       HttpServletRequest request) {
		ModelAndView model = new ModelAndView("json");
		Integer offset = 1;
		if(pageNum!=null&&pageNum!=0){
			offset = pageNum;
		}     
		pageNum = offset;
		PageModel<Account>  page = accountService.listAccount(userId, (pageNum-1)*PAGE_SIZE, PAGE_SIZE);//albumService.listAlbum(siteId,(offset-1)*15, 15);
		model.addObject("json", JsonUtil.objectToJson(page));
		return model;
		
	}
	
	@RequestMapping(value = {"/account/add/{userId}/{email}/{firstname}/{lastname}"}, method = RequestMethod.GET)
	@ResponseBody
	public String addAccount(
			@PathVariable("userId")String userId,   
			@PathVariable("email")String email,      
			@PathVariable("firstname")String firstname,                 
			@PathVariable("lastname")String lastname,                 
			HttpServletRequest request, 
			HttpServletResponse response
		   ){
		
			Message message = new Message();
			Account account = new Account();
			account.setUserid(userId);
			account.setEmail(email);
			account.setFirstname(firstname);
			account.setLastname(lastname);
			try{
				Integer ret = accountService.saveAccount(account);
				if(ret!=1){
					message.failure(-1, "Save Account Fail");
				}
			}catch(Exception e){
				message.failure(-1, e.getMessage());
			}
			
		return wrapMessage(message);
	}
	@RequestMapping(value = "/account/updateById/{id}/{userId}/{firstname}", method = RequestMethod.GET)
	@ResponseBody	
	public String modifyAccount(
			@PathVariable("id") Integer id , 
			@PathVariable("userId") String userId , 
			@PathVariable("firstname") String firstname , 
			HttpServletRequest request, 
			HttpServletResponse response){
		Message message = new Message();
		try{
			Account account = new Account();
			account.setId(id);
			account.setUserid(userId);
			account.setFirstname(firstname);
			Integer ret = accountService.modifyAccount(account);
			if(ret!=1){
				message.failure(-1, "modify Account Fail");
			}
		}catch(Exception e){
			message.failure(-1, e.getMessage());
		}
		return wrapMessage(message);
	}	
	
	@RequestMapping(value = "/account/delByid/{id}/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public String deleteAccountByPk(
			@PathVariable("id") Integer id , 
			@PathVariable("userId") String userId , 
			HttpServletRequest request, 
			HttpServletResponse response){
		Message message = new Message();
		try{
			Integer ret = accountService.deleteAccountByPk(id, userId);
			if(ret!=1){
				message.failure(-1, "delete Account Fail");
			}
		}catch(Exception e){
			message.failure(-1, e.getMessage());
		}
		return wrapMessage(message);
	}
	
	@RequestMapping(value="/free")
	public ModelAndView helloFreeMarker(){
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("freemarker");
		modelAndView.addObject("message", "Hello,FreeMarker");
		return modelAndView;
	}
	
	private String wrapMessage( Message message) {
		return JSONObject.fromObject(message).toString();
	}
}
